Assembly-Level (and Module-Level) Attributes

It is also possible to apply attributes on all types within a given module (for a multi-file assembly; see Chapter 14) or all modules within a given assembly using the [module:] and [assembly:] tags, respectively. For example, assume you wish to ensure that every public member of every public type defined within your assembly is CLS compliant.

Note Chapter 1 mentioned the role of CLS compliant assemblies. Recall that a CLS compliant assembly can be used by all .NET programming languages out-of-the-box. If you create public members of public types which expose non-CLS compliant programming constructs (such as unsigned data or pointer parameters), other .NET languages may not be able to use your functionality. Therefore, if you are building C# code libraries which need to be used by a wide variety of .NET languages, checking for CLS compliance is a must.

To do so, simply add the following assembly level attribute at the very top of any C# source code file. Be very aware that all assembly or module-level attributes must be listed outside the scope of any namespace scope! If you add assembly (or module) level attributes to your project, here is a recommended file-layout to follow:

// List 'using' statements first.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Now list any assembly/module level attributes.
// Enforce CLS compliance for all public types in this assembly.
[assembly: CLSCompliant(true)]

// Now, your namespace(s) and types.
namespace AttributedCarLibrary
{
    // Types...
}

If you now add a bit of code that falls outside the CLS specification (such as an exposed point of unsigned data):

// Ulong types don't jibe with the CLS.
public class Winnebago
{
    public ulong notCompliant;
}

you are issued a compiler warning.

The Visual Studio 2010 AssemblyInfo.cs File

By default, Visual Studio 2010 projects receive a file named AssemblyInfo.cs, which can be viewed by expanding the Properties icon of the Solution Explorer (see Figure 15-6).

Figure 15-6

Figure 15-6 Figure Text

This file is a handy place to put attributes that are to be applied at the assembly level. You may recall from Chapter 14, during the examination of .NET assemblies, that the manifest contains assembly-level metadata, much of which comes from the assembly-level attributes shown in Table 15-4.

Table 15-4. Select Assembly-Level Attributes

Attribute Meaning in Life
[AssemblyCompany] Holds basic company information
[AssemblyCopyright] Holds any copyright information for the product or assembly
[AssemblyCulture] Provides information on what cultures or languages the assembly supports
[AssemblyDescription] Holds a friendly description of the product or modules that make up the assembly
[AssemblyKeyFile] Specifies the name of the file containing the key pair used to sign the assembly (i.e., establish a strong name)
[AssemblyProduct] Provides product information
[AssemblyTrademark] Provides trademark information
[AssemblyVersion] Specifies the assembly’s version information, in the format <major.minor.build.revision>

Source Code The AttributedCarLibrary project is included in the Chapter 15 subdirectory.